<?php
namespace app\admin\model;
use think\Db;
use think\Model;
class AnswerModel extends Model{
    public static function index(){
        return Db::name('qa_category')->order('qc_displayorder desc')->select();
    }
    public static function inquiry(){
        return Db::name('qa_question')->order('time desc')->paginate(10);
    }
    public static function details($id,$number){
        if ($number == 2){
            Db::name('qa_question_copy')->where('qq_id',$id)->delete();
            return Db::name('qa_question')->where('qc_id',$id)->delete();
        }
        $date   =Db::table('yc_qa_question_copy')->where('qq_id',$id)->order('time desc')->select();
        return $date;
    }
    public static function cation($date){
        if (empty($date['qc_name'])){
            return '请输入分类名称';
        }
        $values = self::add($date);
       $category = Db::name('qa_category')->insert($values);
       if ($category){
           return true;
       }
       return '添加失败';
    }
    public static function adverts($id){
        return Db::name('qa_category')->where('qc_id',$id)->find();
    }
    public static function advert($qc_id,$state){
        if ($state == 2){
           return Db::name('qa_category')->where('qc_id',$qc_id)->delete();
        }
        if (empty($qc_id['qc_name'])){
            return '请输入分类名称';
        }
        $values = self::add($qc_id);
        Db::name('qa_category')->where('qc_id',$qc_id['qc_id'])->update($values);
        return true;
    }
    public static function add($date){
        $value = [
            'qc_name'           =>$date['qc_name'],
            'qc_isrecommand'    =>$date['qc_isrecommand'],
            'time'              =>time(),
        ];
        if ($date['qc_displayorder']){
            $value['qc_displayorder'] = $date['qc_displayorder'];
        }
        return $value;
    }
}